import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import get from '@dfeidao/atom-mobile/local/get';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import service from '@dfeidao/atom-mobile/msg/service';
import toast from '@dfeidao/atom-mobile/msgbox/toast';
// import { SegmentedControlIOSComponent } from 'react-native';
import { Alert } from 'react-native';
import { spaceid } from '../atom/config';
export default async function a003(fd: IFeidaoAiMobile) {
	// 同意
	const leave_content = get(fd, 'leave_content');
	const leave_num = get(fd, 'leave_num');
	const productid = get(fd, 'productid');
	const reviewer = get(fd, 'teacher_name');

	// const student_no = get(fd, 'student_no');
	const student_name = get(fd, 'student_name') as string;
	const parent_name = get(fd, 'parent_name') as string;
	// const parent_number = get(fd, 'parent_number');
	const phone = get(fd, 'phone');
	// const phone = '18738555317'; // todo

	try {
		const dx = await nodejs<{ data: { information_template_content: string; }; data1: { openid: string; } }>('app-approval-details/s005', { productid, phone });
		// console.log(dx);
		// 短信模板 openid
		let information_template_content = dx.data.information_template_content;
		information_template_content = information_template_content.replace("@", parent_name);
		information_template_content = information_template_content.replace("@", student_name);
		const openid = dx.data1[0].openid;
		console.log(openid);

		const res = await nodejs<{ code: number }>('app-approval-details/s002', { leave_num, productid, leave_content, reviewer });
		if (res.code === 1) {
			await toast({
				text: '提交成功',
				position: 'top',
				type: 'success'
			});
			const tokn = await service('thirdpart.weixinpublicno', {
				"serviceflag": "getaccesstoken",      // 固定参数
				"spaceid": spaceid,           // 当前项目的spaceid
				"thirdpay_type": "weixin_pubno", // 固定参数（weixin_pubno）
				"productid": productid    // 当前登录用户对应的productid（从系统参数params中获取）
			}) as { access_token: string; };
			const access_token = tokn.access_token;
			console.log(access_token, 'asd');
			try {
				const result = await service('thirdpart.weixinpubno.bulkpushmessage', {
					"tousers": [
						openid       // （3）中取到的openid
					],
					"spaceid": spaceid, // 当前项目的spaceid
					"productid": productid,// 当前登录用户对应的productid（从系统参数params中获取）
					"access_token": access_token,       // （4）返回的token
					"custombody": {
						"msgtype": "text",   // 消息内容（5）中的5.3取到的信息模板内容
						"text": information_template_content
					}
				}) as { code: number; msg: string };
				if (result.code === 1) {
					await toast({
						type: 'success',
						text: result.msg,
						position: 'top'
					});
				}
			} catch (error) {
				Alert.alert(error.message, '推送失败');
			}
		} else {
			await toast({
				text: '提交失败，请稍后重试！',
				position: 'top',
				type: 'danger'
			});
		}
	} catch (error) {
		Alert.alert(error.message);
	}
}
